package com.example.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.RoleUserModel;
import com.example.auth.service.RoleUserModelService;
import com.example.db.DBSql;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.utils.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname RoleUserModelServiceImpl
 * @Description TODO
 * @Date 2022/7/31 15:32
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class RoleUserModelServiceImpl implements RoleUserModelService {

    private static final String ROLE_USER_MODEL=" select  a.id,a.roleId,a.userid,b.username,b.phone,c.rolename,c.roleno,a.createdate\n" +
            " from sys_role_user a LEFT JOIN sys_user b " +
            " ON a.userid=b.id LEFT JOIN sys_role c ON a.roleId=c.id ";

    @Autowired
    TableService tableService;

    @Autowired
    DBSql dbSql;

    @Override
    public IPage<RoleUserModel> page(int pageNum, int pageSize, JSONObject object) {
        IPage<RoleUserModel> iPage=this.pageRoleUserModel(pageNum,pageSize,object);
        return iPage;
    }

    @Override
    public void deleteRoleUserModelById(Object id) {
        tableService.deleteById(id,RoleUserModel.class);
    }

    @Override
    public RoleUserModel saveRoleUserModel(RoleUserModel roleUserModel) {
        tableService.save(roleUserModel,RoleUserModel.class);
        return roleUserModel;
    }

    @Override
    public RoleUserModel getRoleUserModelByUidAndRoleId(Object uid, Object roleId) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(RoleUserModel.class);
        sqlBuilder.and("userid").eq(uid);
        sqlBuilder.and("roleId").eq(roleId);
        String sql=sqlBuilder.getSql();
        RoleUserModel roleUserModel=dbSql.selectMapObj(sql,RoleUserModel.class);
        return roleUserModel;
    }

    @Override
    public void deleteRoleUserModelByUidAndRoleId(Object uid, Object roleId) {
        String sql="delete from sys_role_user where userid='%s' and roleId='%s'";
        sql=String.format(sql,uid,roleId);
        System.out.println(sql);
        dbSql.update(sql);
    }

    @Override
    public List<Integer> getRoleModelRoleIdsByUid(Object uid) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(RoleUserModel.class);
        sqlBuilder.and("userid").eq(uid);
        List<RoleUserModel> roleUserModelList=tableService.selectList(sqlBuilder,RoleUserModel.class);
        if(roleUserModelList==null){
            return Collections.emptyList();
        }
        List<Integer> roleIds=roleUserModelList.stream().map((e)->{
            return e.getRoleId();
        }).collect(Collectors.toList());
        return roleIds;
    }

    @Override
    public RoleUserModel getRoleUserModelById(Object id) {
        return this.getRoleUserModelOneById(id);
    }

    private RoleUserModel getRoleUserModelOneById(Object id){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.select("*").from("("+ROLE_USER_MODEL+") a ");
        sqlBuilder.and("id").eq(id);
        String sql=sqlBuilder.getSql();
        return dbSql.selectMapObj(sql,RoleUserModel.class);
    }

    private IPage<RoleUserModel> pageRoleUserModel(int pageNum, int pageSize, JSONObject obj){
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.select("*").from("( "+ROLE_USER_MODEL+" ) a ");
        if(obj != null){
            Object username=obj.get("username");
            if(!StringUtils.isEmpty(username)){
                sqlBuilder.and("username").like(username);
            }
            Object rolename=obj.get("rolename");
            if(!StringUtils.isEmpty(rolename)){
                sqlBuilder.and("rolename").like(rolename);
            }
            Object roleno=obj.get("roleno");
            if (!StringUtils.isEmpty(roleno)) {
                sqlBuilder.and("roleno").like(roleno);
            }
            Object phone=obj.get("phone");
            if (!StringUtils.isEmpty(phone)) {
                sqlBuilder.and("phone").like(phone);
            }
        }
        sqlBuilder.page(pageNum,pageSize);
        String pageSql=sqlBuilder.getPage();
        String countSql=sqlBuilder.getCountSql();
        List<RoleUserModel> roleUserModelList=dbSql.selectList(pageSql,RoleUserModel.class);
        int count=dbSql.selectCount(countSql);
        IPage<RoleUserModel> iPage=new IPage<RoleUserModel>(count,roleUserModelList);
        return iPage;
    }
}
